package com.lanou3g.musicenjoy.activities;


import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.eventbus.MyEvent;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners;
import com.umeng.socialize.exception.SocializeException;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;

import java.util.Map;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

/**
 * Created by 李宁 on 15/10/20.
 * 登录页面
 */
public class LoginActivity extends FragmentActivity implements View.OnClickListener {
    private UMSocialService umController;
    private String screen_name;// 昵称
    private String profile_image_url;// 头像
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.login)
    LinearLayout login;
    @Bind(R.id.login_qq)
    ImageView loginQq;
    @Bind(R.id.login_weibo)
    ImageView loginWeibo;
    @Bind(R.id.login_phone)
    Button loginPhone;
    @Bind(R.id.login_email)
    Button loginEmail;
    private VideoView video_login;//背景视频
    MediaController mediaco;//视频用
    private int errorTime = 1;
    private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        video_login = (VideoView) findViewById(R.id.video_login);
        loginQq.setOnClickListener(this);
        loginWeibo.setOnClickListener(this);
        back.setOnClickListener(this);

        //背景视频
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mediaco = new MediaController(this);
        String uri = "android.resource://" + getPackageName() + "/" + R.raw.login_video;


        video_login.setVideoURI(Uri.parse(uri));
        video_login.setMediaController(mediaco);
        mediaco.setMediaPlayer(video_login);
        video_login.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
                 mMediaPlayer = mp;
                mp.start();
                mp.setLooping(true);
//                if (logOn) {
//                    Log.e(TAG, "topVideoView.setOnPreparedListener -mp.start()");
//                }
            }
        });
        video_login.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
                if (errorTime < 6) {
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
// TODO Auto-generated method stub
                          //  startTest();
                        }
                    }, 2000);
                    errorTime++;
                    return true;
                }
                return false;
            }
        });

        //让VideoView获取焦点
        video_login.start();
        video_login.requestFocus();
        mediaco.setVisibility(View.GONE);


        // 实现OAuth授权,添加成员变量
        umController = UMServiceFactory.getUMSocialService("com.umeng.login");
        //参数1为当前Activity,参数2为开发者在QQ互联申请的APPID,参数3为开发者在QQ互联申请的APPkEY.
        // 添加QQ平台登录
        UMQQSsoHandler umqqSsoHandler = new UMQQSsoHandler(this, "100424468", "5631edcf67e58e052a000ee2");
        umqqSsoHandler.addToSocialSDK();

        // 添加微博平台登录
        umController.getConfig().setSsoHandler(new SinaSsoHandler());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
                break;
            case R.id.login_qq:
                qqLogin();
                break;
            case R.id.login_weibo:
                weiboLogin();
                break;

        }
    }

    public void qqLogin() {
        // 授权接口
        umController.doOauthVerify(LoginActivity.this, SHARE_MEDIA.QQ, new SocializeListeners.UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                Toast.makeText(LoginActivity.this, "授权开始", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onComplete(Bundle bundle, SHARE_MEDIA share_media) {
                Toast.makeText(LoginActivity.this, "授权完成", Toast.LENGTH_LONG).show();
                // 获取相关授权信息
                umController.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.QQ, new SocializeListeners.UMDataListener() {
                    @Override
                    public void onStart() {
                        Toast.makeText(LoginActivity.this, "获取平台数据开始...", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onComplete(int status, Map<String, Object> info) {
                        if (status == 200 && info != null) {
                            StringBuilder stringBuilder = new StringBuilder();
                            Set<String> keys = info.keySet();
                            for (String key : keys) {
                                stringBuilder.append(key + " = " + info.get(key).toString() + "\r\n");
                            }
                            Log.d("TestData", stringBuilder.toString());
                            getQQData(info);


                        } else {
                            Log.d("TestData", "发生错误:" + status);
                        }
                    }
                });
            }

            @Override
            public void onError(SocializeException e, SHARE_MEDIA share_media) {
                Toast.makeText(LoginActivity.this, "授权错误", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                Toast.makeText(LoginActivity.this, "授权取消", Toast.LENGTH_LONG).show();
            }
        });
    }

    private void getQQData(Map<String, Object> info) {
        // 得到授权者的昵称,头像,性别
        screen_name = info.get("screen_name").toString();
        profile_image_url = info.get("profile_image_url").toString();

//        Intent data = new Intent();
//        data.putExtra("screen_name", screen_name);
//        data.putExtra("profile_image_url", profile_image_url);
//
//        setResult(77, data);
        //用EventBus来做中间站
        EventBus.getDefault().post(new MyEvent.Landing(info));
        finish();

    }

    public void weiboLogin() {
        umController.doOauthVerify(LoginActivity.this, SHARE_MEDIA.SINA, new SocializeListeners.UMAuthListener() {
            @Override
            public void onStart(SHARE_MEDIA share_media) {
                Toast.makeText(LoginActivity.this, "授权开始", Toast.LENGTH_LONG).show();
            }


            @Override
            public void onComplete(Bundle bundle, SHARE_MEDIA share_media) {
                Toast.makeText(LoginActivity.this, "授权完成", Toast.LENGTH_LONG).show();
                // 获取相关授权信息
                umController.getPlatformInfo(LoginActivity.this, SHARE_MEDIA.SINA, new SocializeListeners.UMDataListener() {
                    @Override
                    public void onStart() {
                        Toast.makeText(LoginActivity.this, "获取平台数据开始...", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onComplete(int status, Map<String, Object> info) {
                        if (status == 200 && info != null) {
                            StringBuilder stringBuilder = new StringBuilder();
                            Set<String> keys = info.keySet();
                            for (String key : keys) {
                                stringBuilder.append(key + " = " + info.get(key).toString() + "\r\n");
                            }
                            getWeiBoData(info);
                        } else {
                            Log.d("TestData", "发生错误:" + status);
                        }
                    }
                });
            }

            @Override
            public void onError(SocializeException e, SHARE_MEDIA share_media) {
                Toast.makeText(LoginActivity.this, "授权错误", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancel(SHARE_MEDIA share_media) {
                Toast.makeText(LoginActivity.this, "授权取消", Toast.LENGTH_LONG).show();
            }
        });
    }

    public void getWeiBoData(Map<String, Object> info) {
        // 得到授权者的昵称,头像,性别和城市
        screen_name = info.get("screen_name").toString();
        Log.i("fjkjsklfj", screen_name);
        profile_image_url = info.get("profile_image_url").toString();

//        Intent data = new Intent();
//        data.putExtra("screen_name", screen_name);
//        data.putExtra("profile_image_url", profile_image_url);
//
//        setResult(42, data);
        //用eventBus来做中间站
        EventBus.getDefault().post(new MyEvent.weiboLanding(info));
        // 关闭当前Activity
        finish();
    }
//    @Override
//    public boolean onCreateOptionsMenu( menu) {
//        getMenuInflater().inflate(R.raw.login_video, raw);
//        return true;
//    }
}
